package com.xlhj.yucloud.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.xlhj.yucloud.web.service.LoginService;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author: liucaijing
 * @description: MybatisPlus处理类
 * @date: 2021/9/10 12:55
 */
@Component
@RequiredArgsConstructor
public class MybatisPlusHandler implements MetaObjectHandler {

    private final LoginService loginService;

    /**
     * 配置创建时间自动填充
     * @param metaObject
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        this.strictInsertFill(metaObject, "createTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictInsertFill(metaObject, "createUser", () -> loginService.getUserInfo().getUserName(), String.class);
        this.strictInsertFill(metaObject, "updateUser", () -> loginService.getUserInfo().getUserName(), String.class);
    }

    /**
     * 配置更新时间自动填充
     * @param metaObject
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        this.strictUpdateFill(metaObject, "updateTime", () -> LocalDateTime.now(), LocalDateTime.class);
        this.strictUpdateFill(metaObject, "updateUser", () -> loginService.getUserInfo().getUserName(), String.class);
    }
}
